home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / FlashingGraphics.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.0 KB  |  174 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This applet animates graphics that it generates.  This example
  22.  * isn't a good one to copy -- it flashes.  The next couple of examples 
  23.  * will show how to eliminate the flashing.
  24.  */
  25.  
  26. public class FlashingGraphics extends Applet implements Runnable {
  27.     int frameNumber = -1;
  28.     int delay;
  29.     Thread animatorThread;
  30.     boolean frozen = false;
  31.  
  32.     int squareSize = 20;
  33.     boolean fillColumnTop = true;
  34.  
  35.     public void init() {
  36.         String str;
  37.         int fps = 10;
  38.  
  39.         //How many milliseconds between frames?
  40.         str = getParameter("fps");
  41.         try {
  42.             if (str != null) {
  43.                 fps = Integer.parseInt(str);
  44.             }
  45.         } catch (Exception e) {}
  46.         delay = (fps > 0) ? (1000 / fps) : 100;
  47.  
  48.         //How many pixels wide is each square?
  49.         str = getParameter("squareWidth");
  50.         try {
  51.             if (str != null) {
  52.                 squareSize = Integer.parseInt(str);
  53.             }
  54.         } catch (Exception e) {}
  55.     }
  56.  
  57.     public void start() {
  58.         if (frozen) {
  59.             //Do nothing.  The user has requested that we
  60.             //stop changing the image.
  61.         } else {
  62.             //Start animating!
  63.             if (animatorThread == null) {
  64.                 animatorThread = new Thread(this);
  65.             }
  66.             animatorThread.start();
  67.         }
  68.     }
  69.  
  70.     public void stop() {
  71.         //Stop the animating thread.
  72.         animatorThread = null;
  73.     }
  74.  
  75.     public boolean mouseDown(Event e, int x, int y) {
  76.         if (frozen) {
  77.             frozen = false;
  78.             start();
  79.         } else {
  80.             frozen = true;
  81.             stop();
  82.         }
  83.         return true;
  84.     }
  85.  
  86.     public void run() {
  87.         //Just to be nice, lower this thread's priority
  88.         //so it can't interfere with other processing going on.
  89.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  90.  
  91.         //Remember the starting time.
  92.         long startTime = System.currentTimeMillis();
  93.  
  94.         //This is the animation loop.
  95.         while (Thread.currentThread() == animatorThread) {
  96.             //Advance the animation frame.
  97.             frameNumber++;
  98.  
  99.             //Display it.
  100.             repaint();
  101.  
  102.             //Delay depending on how far we are behind.
  103.             try {
  104.                 startTime += delay;
  105.                 Thread.sleep(Math.max(0, 
  106.                                       startTime-System.currentTimeMillis()));
  107.             } catch (InterruptedException e) {
  108.                 break;
  109.             }
  110.         }
  111.     }
  112.  
  113.     public void paint(Graphics g) {
  114.         Dimension d = size();
  115.         boolean fillSquare;
  116.         boolean fillNextFrame;
  117.         int rowWidth = 0;
  118.         int x = 0, y = 0;
  119.         int w, h;
  120.         int tmp;
  121.  
  122.         //Set width of first "square". Decide whether to fill it.
  123.         fillSquare = fillColumnTop;
  124.         fillColumnTop = !fillColumnTop;
  125.         tmp = frameNumber % squareSize;
  126.         if (tmp == 0) {
  127.             w = squareSize;
  128.             fillNextFrame = !fillSquare;
  129.         } else {
  130.             w = tmp;
  131.             fillNextFrame = fillSquare;
  132.         }
  133.  
  134.         //Draw from left to right.
  135.         while (x < d.width) {
  136.             int colHeight = 0;
  137.  
  138.             //Draw the column.
  139.             while (y < d.height) {
  140.                 colHeight += squareSize;
  141.  
  142.                 //If we don't have room for a full square, cut if off.
  143.                 if (colHeight > d.height) {
  144.                     h = d.height - y;
  145.                 } else {
  146.                     h = squareSize;
  147.                 }
  148.  
  149.                 //Draw the rectangle if necessary.
  150.                 if (fillSquare) {
  151.                     g.fillRect(x, y, w, h);
  152.                     fillSquare = false;
  153.                 } else {
  154.                     fillSquare = true;
  155.                 } 
  156.  
  157.                 y += h;
  158.             } //while y
  159.  
  160.             //Determine x, y, and w for the next go around.
  161.             x += w;
  162.             y = 0;
  163.             w = squareSize;
  164.             rowWidth += w;
  165.             if (rowWidth > d.width) {
  166.                 w = d.width - x;
  167.             }
  168.             fillSquare = fillColumnTop;
  169.             fillColumnTop = !fillColumnTop;
  170.         } //while x
  171.         fillColumnTop = fillNextFrame;
  172.     }
  173. }
  174.